/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-7-28 下午3:08 Created by lyyang
 */
package com.jade.bss.security.shiro.authc;

import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.customer.CustomerManager;
import com.jade.framework.base.context.ApplicationContextUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * 验证码realm
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-7-28 下午3:08
 */
public class VerifyCodeRealm extends AuthorizingRealm
{
    private CustomerManager customerManager = ApplicationContextUtils.getBean("bss_customerManager");

    public VerifyCodeRealm()
    {
        setAuthenticationTokenClass(VerifyCodeToken.class);
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
    {
        return new SimpleAuthorizationInfo();
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken)
            throws AuthenticationException
    {
        VerifyCodeToken token = (VerifyCodeToken) authenticationToken;
        Customer customer = customerManager.getCustomer(token.getType(), token.getUsername());
        if (customer == null) {
            return null;
        }
        return new SimpleAuthenticationInfo(customer, customer.getPassword(), getName());
    }
}
